package cn.edu.zstu.overlappingdisappear.base;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author ZhuMingWei(986990501@qq.com)
 * @Date on 2019/11/7
 * @Version v1.0.0
 * @Company TODO
 * @Describle 本地信息
 */

public class LocalHelper {

    private static SharedPreferences preferences = MyApplication.getInstance().getSharedPreferences("userinfo", Context.MODE_PRIVATE);
    private static SharedPreferences.Editor editor = preferences.edit();

    private static String IsLogin = "IS_LOGIN";
    private static boolean defIsLogin = false;

    private static String Username = "USERNAME";
    private static String defUsername = "";

    private static String Nickname = "NICKNAME";
    private static String defNickname = "";

    private static String CurrentLevelEasy = "CURRENT_LEVEL_EASY";
    private static int defCurrentLevelEasy = 1;

    private static String CurrentLevelNormal = "CURRENT_LEVEL_NORMAL";
    private static int defCurrentLevelNormal = 1;

    private static String CurrentLevelDifficult = "CURRENT_LEVEL_Difficult";
    private static int defCurrentLevelDifficult = 1;

    /********************** value --> String ***********************/
    public static void setUserInfo(String username,String nickname) {
        putString(Username,username);
        putString(Nickname,nickname);
    }
    public static String getUsername() {
        return getString(Username,defUsername);
    }
    public static String getNickname() {
        return getString(Nickname,defNickname);
    }
    /********************** value --> Int ***********************/
    public static void setCurLevel(int dfficult,int level) {
        switch (dfficult){
            case 1:
                putInt(CurrentLevelEasy,level);
                break;
            case 2:
                putInt(CurrentLevelNormal,level);
                break;
            case 3:
                putInt(CurrentLevelDifficult,level);
                break;
        }
    }
    public static int getCurLevel(int dfficult) {
        switch (dfficult){
            case 1:
                return getInt(CurrentLevelEasy,defCurrentLevelEasy);
            case 2:
                return getInt(CurrentLevelNormal,defCurrentLevelNormal);
            case 3:
                return getInt(CurrentLevelDifficult,defCurrentLevelDifficult);
            default:
                return 0;
        }
    }

    /********************** value --> boolean ***********************/
    public static void setIsLogin(boolean isLogin) {
        putBoolean(IsLogin,isLogin);
    }
    public static boolean getIsLogin() {
        return getBoolean(IsLogin,defIsLogin);
    }
    /************************** 保存和读取基本类型的数据 *****************************/
    /**
     * 保存值为 String 类型的数据
     *
     * @param key
     * @param value
     */
    public static void putString(String key, String value) {
        editor.putString(key, value);
        editor.apply();
    }

    public static String getString(String key, String defValue) {
        return preferences.getString(key, defValue);
    }

    /**
     * 保存值为 boolean 类型的数据
     *
     * @param key
     * @param value
     */
    public static void putBoolean(String key, boolean value) {
        editor.putBoolean(key, value);
        editor.apply();
    }

    public static boolean getBoolean(String key, boolean defvalue) {
        return preferences.getBoolean(key, defvalue);
    }
    /**
     * 保存值为 int 类型的数据
     *
     * @param key
     * @param value
     */
    public static void putInt(String key, int value) {
        editor.putInt(key, value);
        editor.apply();
    }

    public static int getInt(String key, int defValue) {
        return preferences.getInt(key, defValue);
    }

    /**
     * 保存和读取List<JavaBean>
     * 本质上就是：借助Gson把List集合转成Json字符串存到SharedPreference中;在取的时候，先取出该Json字符串，然后再转成List<JavaBean>
     */
    public static <T> void putList(String tag, List<T> datalist) {
        if (null == datalist || datalist.size() <= 0)
            return;
        Gson gson = new Gson();
        //转换成json数据，再保存
        String strJson = gson.toJson(datalist);
        editor.putString(tag, strJson);
        editor.apply();
    }

    public static <T>void putBean(String tag, T bean){
        if(null==bean){
            return;
        }
        Gson gson=new Gson();
        String jsonStr=gson.toJson(bean);
        editor.putString(tag,jsonStr);
        editor.apply();
    }

    public static <T> T getBean(String tag,Class<T> clazz){
        T bean =new Gson().fromJson(getString(tag,null),clazz);
        return bean;
    }

    public static <T> T getBean(String tag,Class<T> clazz,T defValue){
        T bean =new Gson().fromJson(getString(tag,new Gson().toJson(defValue)),clazz);
        return bean;
    }

    /**
     * @param tag          key
     * @param defaultValue 默认值
     * @param clazz        javaBean数组的字节码。eg:Student[].class
     * @return
     */
    private static <T> List<T> getList(String tag, String defaultValue, Class<T[]> clazz) {
        List<T> datalist = new ArrayList<T>();
        String jsonStr = preferences.getString(tag, defaultValue);
        if (null == jsonStr) {
            return datalist;
        }

        T[] arr = new Gson().fromJson(jsonStr, clazz);
        return Arrays.asList(arr);
    }

    /**
     * 从sp中移除指定节点
     *
     * @param key
     */
    public static void remove(String key) {
        editor.remove(key).apply();
    }


    /**
     * 清除SharedPreference
     */
    public static void clearSharedPreference(){
        editor.clear();
        editor.apply();
    }
}
